//
//  HosListViewController.m
//  Doctor1.0
//
//  Created by mk on 15/11/20.
//  Copyright (c) 2015年 cocoa-mk. All rights reserved.
//

#import "HosListViewController.h"
#import "Doctor.h"
#import "HosListCell.h"
#import "NewReservationListViewController.h"

@interface HosListViewController ()<HostListCellDelegate>
{
    DoctorListData * doctorList;
    NSMutableDictionary * dicHosData;//医院信息
}
@end

@implementation HosListViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    titleLabel.text = self.strHosName;
    dicHosData = [[NSMutableDictionary alloc]initWithCapacity:0];
    
    doctorList = [[DoctorListData alloc]init];
    
    [tableMain becomeRefreshing:YES];    
}

-(void)getData
{
    doctorList.urlPath = [NSString stringWithFormat:@"%@%@",self.strUrl,self.strHosID];
    doctorList.strDataKey = @"doctors";
    
    [doctorList loadPageData:^(AFHTTPRequestOperation *operation, id responseObject, BOOL haveNextPage)
    {
       // NSLog(@"==%@",responseObject);
        dicHosData = [[responseObject objectForKey:requestDataKey] objectForKey:@"department"];
        [tableMain reloadData];
        
        [tableMain refreshFinished];
        tableMain.loadMoreView.state = (haveNextPage?LOADMORENORMAL:LOADMOREFINISHED);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         [tableMain refreshFinished];
         tableMain.loadMoreView.state = LOADMORENORMAL;
    }];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  doctorList.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * strCell = @"HosListCellStr";
    
    HosListCell * cell = (HosListCell *)[tableView dequeueReusableCellWithIdentifier:strCell];
    if (!cell)
    {
        cell =  (HosListCell *) [[[NSBundle mainBundle]loadNibNamed:@"HosListCell" owner:self options:nil] objectAtIndex:0];
    }
    
    [cell fillDoctorDataWith:[doctorList.dataArray objectAtIndex:indexPath.row]];
    [cell layoutIfNeeded];
    [cell updateConstraintsIfNeeded];
    
    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    
    return height;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString * strCell = @"HosListCellStr";
    HosListCell * cell = (HosListCell *)[tableView dequeueReusableCellWithIdentifier:strCell];
    if (!cell)
    {
        cell =  (HosListCell *)[[[NSBundle mainBundle]loadNibNamed:@"HosListCell" owner:self options:nil] objectAtIndex:0];
        cell.delegate = self;
    }
    
    [cell fillDoctorDataWith:[doctorList.dataArray objectAtIndex:indexPath.row]];
    
    return cell;
}


-(void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    [tableMain refreshFinished];
    [self getData];

}


-(void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView
{
    [tableMain setCanLoadMore:YES];
    doctorList.requestPage = kLoadMoreStartPage;
    doctorList.strLastId = @"1";
    [self getData];
    
}


#pragma mark CellDelegate
-(void)selectDoctorWith:(HosListCell *)cell
{
    if (![gProjConfig needLogin])
    {
        NSIndexPath * index = [tableMain indexPathForCell:cell];
        NewReservationListViewController * ewse = [[NewReservationListViewController alloc]initWithNibName:@"NewReservationListViewController" bundle:nil];
        Doctor * doc = [doctorList.dataArray objectAtIndex:index.row];
        doc.hpName = [dicHosData objectForKey:@"hpName"];//医院名字
        ewse.doctorData = doc;
       // NSLog(@"%@",ewse.doctorData.hpDeptName);
        ewse.reservationDoctor = YES;
        [self.navigationController pushViewController:ewse animated:YES];
    }
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
